//
//  SelectButton.m
//  Honda
//
//  Created by Wang.Xinlei on 4/28/13.
//  Copyright (c) 2013 HeninHoo. All rights reserved.
//

#import "SelectButton.h"

@implementation SelectButton

- (void)dealloc {
    [self removeObserver:self forKeyPath:@"frame"];
    
    [super dealloc];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"frame"]) {
        [self setTitleColor:[UIColor colorWithWhite:.62 alpha:1] forState:UIControlStateNormal];
        [self setBackgroundImage:[[UIImage imageNamed:@"button_normal"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                        forState:UIControlStateNormal];
        
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [self setBackgroundImage:[[UIImage imageNamed:@"button_highlighted"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                        forState:UIControlStateHighlighted];
        
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [self setBackgroundImage:[[UIImage imageNamed:@"button_selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                        forState:UIControlStateSelected];
        
        
        [self setTitleColor:[UIColor colorWithWhite:.62 alpha:1] forState:UIControlStateDisabled];
        [self setBackgroundImage:[[UIImage imageNamed:@"button_disabled"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                        forState:UIControlStateDisabled];
    }
}

- (void)awakeFromNib {
    [self addTarget:self action:@selector(onTapSelectButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
    
    self.backgroundColor = [UIColor clearColor];
    self.titleLabel.font = [UIFont systemFontOfSize:18];
    
    self.frame = self.frame;
}

#pragma mark
- (void)onTapSelectButton:(UIButton *)button {
    self.selected = !self.selected;
}

@end
